package com.springbootorder.feignservice.impl;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.springbootorder.entity.Order;
import com.springbootorder.feignservice.OrderFeignService;
import com.springbootorder.mapper.OrderMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderFeignServiceImpl implements OrderFeignService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<Order> listData(Order product) {
        return orderMapper.findAll(product);
    }

    //事物执行方
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS) //分布式事务注解
    @Transactional //本地事务注解
    @Override
    public int updateByOrder(Order product){
        try {
            return  orderMapper.updateByPrimaryKeySelective(product);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    //事物的参与方
    @LcnTransaction(propagation = DTXPropagation.SUPPORTS)
    @Transactional
    @Override
    public int save(Order product){
        try {
            return orderMapper.insertSelective(product);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
